﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApp.Data;

namespace ConsoleApp
{
    public class RepositoryCategory : IRepositoryCategory
    {
        private StandardsEntities db = new StandardsEntities();

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<Data.Category> GetAll()
        {
            return db.Categories.ToList<Category>();
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public int Create(Category c)
        {
            try
            {
                db.Categories.AddObject(c);
                db.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        public int Delete(string cateId)
        {
            try
            {
                Category c = db.Categories.SingleOrDefault(x=>x.CategoryId==Convert.ToInt32(cateId));
                db.Categories.DeleteObject(c);
                db.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public int Edit(Category c)
        {
            try
            {
                Category cateUp = db.Categories.SingleOrDefault(x => x.CategoryId == c.CategoryId);
                cateUp.CategoryName = c.CategoryName;
                db.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        public Category Details(string cateId)
        {
            return db.Categories.SingleOrDefault(x => x.CategoryId == Convert.ToInt32(cateId));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<Category> FindByName(string name)
        {
            return db.Categories.Where(x => x.CategoryName.ToLower().Contains(name)).ToList<Category>();
        }
    }
}
